package LeetCode.LeetCode2;

/**
 * @author lumin
 * @date 2022/04/18 21:01
 **/
public class Num63 {
    public int uniquePathsWithObstacles(int[][] obstacleGrid) {
        int row = obstacleGrid.length;
        int col = obstacleGrid[0].length;
        if(row == 1 && col == 1 && obstacleGrid[0][0] == 1){
            return 0;
        }
        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                if(obstacleGrid[i][j] == 1){
                    obstacleGrid[i][j] = -1;
                }
                if(obstacleGrid[i][j] != -1){
                    obstacleGrid[i][j] = 1;
                }
            }
        }
        int i = 1;
        while(i < row){
            int j = 1;
            while(j < col){
                if(obstacleGrid[i - 1][j] == -1){
                    obstacleGrid[i][j] = Math.max(obstacleGrid[i][j - 1],obstacleGrid[i][j]);
                }else if(obstacleGrid[i][j - 1] == -1){
                    obstacleGrid[i][j] = Math.max(obstacleGrid[i][j - 1],obstacleGrid[i][j]);
                }else {
                    obstacleGrid[i][j] = obstacleGrid[i][j] + Math.min(obstacleGrid[i - 1][j], obstacleGrid[i][j - 1]);
                }
                j++;
            }
            i++;
        }
        return obstacleGrid[row - 1][col - 1];
    }
}